﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;

namespace SoftwareEngineering_Project
{
    public partial class Users : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            User user = new User();
            if (!Page.IsPostBack)
            {
                MyUser.DataSource = user.getUsers("Select * from SysUser,Employee Where SysUser.Employee_ID = Employee.Employee_ID");
                MyUser.DataTextField = "UserName";
                //MyUser.DataTextField += " LName";
                MyUser.DataValueField = "UserName";
                MyUser.DataBind();
                GridView1.DataSource = user.getUsers("Select UserName,FName,LName,PasswordHASH,AStatus from SysUser,Employee Where SysUser.Employee_ID = Employee.Employee_ID ");
                GridView1.DataBind();

                Password.Text = user.getCurrent("Select PasswordHASH from SysUser Where UserName ='" + MyUser.Text + "'");
                Mytype.Text = user.getCurrent("Select RoleName from SysUser,UserRole where SysUser.Role_ID = UserRole.Role_ID and UserName ='" + MyUser.Text + "'");
            }
            

        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            User user =new User();
            Password.Text = user.getCurrent("Select PasswordHASH from SysUser Where UserName ='" + MyUser.Text + "'");
            Mytype.Text = user.getCurrent("Select RoleName from SysUser,UserRole where SysUser.Role_ID = UserRole.Role_ID and UserName ='" + MyUser.Text + "'");
        }

        protected void Password_TextChanged(object sender, EventArgs e)
        {

        }

        protected void UpdateUser_Click(object sender, EventArgs e)
        {
            int myroleID;
            if (Mytype.Text == "Ambassador")
            {
                myroleID = 11;
            }
            else
            {
                myroleID = 12;
            }
          
            User user = new User();
            if (user.ExcuteNoresultsQuery("UPDATE SysUser SET PasswordHASH='" + Password.Text + "' , Role_ID=" + myroleID +" WHERE (UserName='" + MyUser.Text + "')"))
            {
                (new Logs()).CreateLog("5", "");
                Errormessage.Visible = true;
                Errormessage.Text = "User information were updated successfully";
            }
            else
            {
                Errormessage.Visible = true;
                Errormessage.Text = "Problem accessing the Database";
            }
            Response.AddHeader("REFRESH", "2;URL=ManageUsers.aspx");
        }

        protected void DeleteUser_Click(object sender, EventArgs e)
        {
            User user = new User();
            user.ExcuteNoresultsQuery("Delete from SysUser where UserName='" + MyUser.Text + "'");
            (new Logs()).CreateLog("6", "");
            Errormessage.Visible = true;
            Errormessage.Text = "User information were deleted successfully";
            Response.AddHeader("REFRESH", "2;URL=ManageUsers.aspx");

            
        }
    }
}